package io.renren.modules.lanling.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.lanling.entity.SubjectEntity;
import io.renren.modules.lanling.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static io.renren.modules.sys.shiro.ShiroUtils.getUserEntity;
import static io.renren.modules.sys.shiro.ShiroUtils.getUserId;


/**
 * 科目表
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-10-31 11:45:32
 */
@RestController
@RequestMapping("lanling/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private ExamTemplateService examTemplateService;
    @Autowired
    private StuSubService stuSubService;
    @Autowired
    private QuestionErrorService questionErrorService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private SchoolService schoolService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:subject:list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = getUserId();
        params.put("createUserId", userId);
        PageUtils page = subjectService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lanling:subject:info")
    public R info(@PathVariable("id") Integer id) {
        SubjectEntity subject = subjectService.selectById(id);
        return R.ok().put("subject", subject);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:subject:save")
    public R save(@RequestBody SubjectEntity subject) {
        Long userId = getUserId();
        subject.setCreateUserId(userId);
        //查找lanling_school中id
        int schoolId = schoolService.querySchoolId(userId);
        subject.setSchoolId(schoolId);
        subject.setCreateDate(new Date());
        subjectService.insert(subject);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:subject:update")
    public R update(@RequestBody SubjectEntity subject) {
        ValidatorUtils.validateEntity(subject);
        subject.setLastModifyDate(new Date());
        //全部更新
        subjectService.updateAllColumnById(subject);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:subject:delete")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody Integer[] ids) {

        for (int id : ids) {
            //删除lanling_exam_template的关联
            examTemplateService.deleteBySubjectId(id);
            //删除lanling_stu_sub的关联
            stuSubService.deleteBySubjectId(id);
            //删除 lanling_question_error 的关联
            questionErrorService.deleteBySubjectId(id);
            //删除 lanling_question 的关联
            questionService.deleteBySubjectId(id);
        }
        subjectService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }


    @RequestMapping("/queryList")
    public R queryList() {
        Long userId = getUserId();

        EntityWrapper<SubjectEntity> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("create_user_id", userId);
        entityWrapper.orderBy("sort_order", false);

        List<SubjectEntity> subjectEntities = subjectService.selectList(entityWrapper);
        return R.ok().put("list", subjectEntities);
    }

    @RequestMapping("/queryListByQues")
    public R queryListByQues() {
        Long userId = getUserId();
        return R.ok().put("list", subjectService.queryListByQues(userId));
    }


}
